package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tianxia.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/*
 * 天下信用报告记录-失信
 *
 * @author:szw
 * */

public class TxCreditShiXinParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 天下信用报告记录-失信
     *
     * @param context
     * @param model
     * @param successful
     * @return boolean
     * @throws IOException
     * @throws InterruptedException
     * @author typole
     * @date 2020/12/01 5:08 下午
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TxCreditReportRecord obj = (TxCreditReportRecord) model;
        TxCreditReportData data = obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);

        FinanceBreakFailthInfo financeBreakFailthInfo = data.getFinanceBreakFailthInfo();
        if (financeBreakFailthInfo != null) {
            List<FinanceBreakFailthInfoData> financeBreakFailthInfoDataList = financeBreakFailthInfo.getData() == null ? new ArrayList<>() : financeBreakFailthInfo.getData();
            if (!financeBreakFailthInfoDataList.isEmpty()) {
                for (FinanceBreakFailthInfoData fbf : financeBreakFailthInfoDataList) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(fbf.getDate() == null ? "" : fbf.getDate()).append(CRT001);
                    TxSb.append(fbf.getType() == null ? "" : fbf.getType()).append(CRT001);
                    TxSb.append(fbf.getCourt() == null ? "" : fbf.getCourt()).append(CRT001);
                    TxSb.append("financeBreakFailthInfo").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }

        TransportationBlackListInfo transportationBlackListInfo = data.getTransportationBlackListInfo();
        if (transportationBlackListInfo != null) {
            List<TransportationBlackListInfoData> transportationBlackListInfoDataList = transportationBlackListInfo.getData() == null ? new ArrayList<>() : transportationBlackListInfo.getData();
            if (!transportationBlackListInfoDataList.isEmpty()) {
                for (TransportationBlackListInfoData tb : transportationBlackListInfoDataList) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(tb.getDate() == null ? "" : tb.getDate()).append(CRT001);
                    TxSb.append(tb.getType() == null ? "" : tb.getType()).append(CRT001);
                    TxSb.append(tb.getCourt() == null ? "" : tb.getCourt()).append(CRT001);
                    TxSb.append("transportationBlackListInfo").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }

        return financeBreakFailthInfo != null || transportationBlackListInfo != null;
    }

    @Override
    public String getSubType() {
        return ".shixin";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tx_credit_shixin_inc";
    }

    @Override
    public String getType() {
        return RecordType.TX_CREDIT_REPORT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "天下信用-失信&限制出行";
    }
}
